package com.ssm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ssm.entity.YlhBaoqi;
import com.ssm.service.YlhService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 运粮河曝气业务类实现
 */
@Service
@Slf4j
public class YlhServiceImpl implements YlhService {

    //运粮河河段1的曝气个数
    private static final int YLHXY01NUMBER = 2;
    //运粮河河段2的曝气个数
    private static final int YLHXY02NUMBER = 4;
    //运粮河河段3的曝气个数
    private static final int YLHXY03NUMBER = 2;
    //运粮河河段4的曝气个数
    private static final int YLHXY04NUMBER = 3;
    //运粮河河段5的曝气个数
    private static final int YLHXY05NUMBER = 3;
    //运粮河河段6的曝气个数
    private static final int YLHXY06NUMBER = 3;
    //龙河河段1曝气个数
    private static final int LH01NUMBER = 3;
    //龙河河段2曝气个数
    private static final int LH02NUMBER = 4;
    //龙河河段3曝气个数
    private static final int LH03NUMBER = 3;
    //龙河河段4曝气个数
    private static final int LH04NUMBER = 2;
    //龙河河段5曝气个数
    private static final int LH05NUMBER = 5;
    //龙河河段6曝气个数
    private static final int LH06NUMBER = 3;
    //龙河河段7曝气个数
    private static final int LH07NUMBER = 2;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 中转发送报文
     * @param content
     * @return boolean
     */
    @Override
    public void sendContent(String content) {

        if (content.length() == 28) {
            sendYlhContent(content);
        }
        if (content.length() == 22) {
            sendLhContent(content);
        }

    }

    /**
     * 发送运粮河报文
     * @param content
     */
    private void sendYlhContent(String content) {
        List<YlhBaoqi> ylhBaoqis = setContent(content);
        log.info("运粮河报文解析为: {}", JSON.toJSONString(ylhBaoqis));
        ResponseEntity<String> response = restTemplate.postForEntity("http://112.126.97.123:8080/suzhou_baoqi-1.0-SNAPSHOT/baoqi/ylhInsert",
                ylhBaoqis, String.class);
        JSONObject obj = JSON.parseObject(response.getBody());
        int code = (int)obj.get("code");
        if (code != 200) {
            throw new RuntimeException("发送报文失败");
        }
    }

    /**
     * 发送龙河报文
     * @param content
     */
    private void sendLhContent(String content) {
        List<YlhBaoqi> lhBaoqis = setLhBaoqi(content);
        log.info("龙河报文解析为: {}", JSON.toJSONString(lhBaoqis));
        ResponseEntity<String> response = restTemplate.postForEntity("http://112.126.97.123:8080/suzhou_baoqi-1.0-SNAPSHOT/baoqi/lhInsert",
                lhBaoqis, String.class);
        JSONObject obj = JSON.parseObject(response.getBody());
        int code = (int)obj.get("code");
        if (code != 200) {
            throw new RuntimeException("发送报文失败");
        }
    }





    /**
     * 解析运粮河报文
     * @param content
     * @return List
     */
    private List<YlhBaoqi> setContent(String content) {
        //截取设备标记部分
        String stationCode = StringUtils.substring(content, 0, 14);
        //截取状态值部分
        String statusCode = StringUtils.substring(content, 20, 24);

        Date now = new Date();
        //根据设备表记部分判断-河段1
        if (StringUtils.equals("594c4858593031", stationCode)) {

            String stationPrefix = "YLHXY01";

            //判断状态值部分
            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, YLHXY01NUMBER, false, now, content);
            }
            if (StringUtils.equals("0500", statusCode) || StringUtils.equals("0a00", statusCode)) {
                return setBaoqiList(stationPrefix, YLHXY01NUMBER, true, now, content);
            }
            if (StringUtils.equals("0100", statusCode) || StringUtils.equals("0200", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
            if (StringUtils.equals("0400", statusCode) || StringUtils.equals("0800", statusCode)) {
                return setBaoqiList(stationPrefix + "_2", now, content);
            }

        }
        //根据设备表记部分判断-河段2
        if (StringUtils.equals("594c4858593032", stationCode)) {
            String stationPrefix = "YLHXY02";

            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, YLHXY02NUMBER, false, now, content);
            }
            if (StringUtils.equals("5500", statusCode) || StringUtils.equals("aa00", statusCode)) {
                return setBaoqiList(stationPrefix, YLHXY02NUMBER, true, now, content);
            }
            if (StringUtils.equals("0400", statusCode) || StringUtils.equals("0800", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
            if (StringUtils.equals("0100", statusCode) || StringUtils.equals("0200", statusCode)) {
                return setBaoqiList(stationPrefix + "_2", now, content);
            }
            if (StringUtils.equals("1000", statusCode) || StringUtils.equals("2000", statusCode)) {
                return setBaoqiList(stationPrefix + "_3", now, content);
            }
            if (StringUtils.equals("4000", statusCode) || StringUtils.equals("8000", statusCode)) {
                return setBaoqiList(stationPrefix + "_4", now, content);
            }
        }

        //根据设备表记部分判断-河段3
        if (StringUtils.equals("594c4858593033", stationCode)) {
            String stationPrefix = "YLHXY03";

            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, YLHXY03NUMBER, false, now, content);
            }
            if (StringUtils.equals("0500", statusCode) || StringUtils.equals("0a00", statusCode)) {
                return setBaoqiList(stationPrefix, YLHXY03NUMBER, true, now, content);
            }
            if (StringUtils.equals("0400", statusCode) || StringUtils.equals("0800", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
            if (StringUtils.equals("0100", statusCode) || StringUtils.equals("0200", statusCode)) {
                return setBaoqiList(stationPrefix + "_2", now, content);
            }
        }
        //根据设备表记部分判断-河段4
        if (StringUtils.equals("594c4858593034", stationCode)) {
            String stationPrefix = "YLHXY04";

            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, YLHXY04NUMBER, false, now, content);
            }
            if (StringUtils.equals("1401", statusCode) || StringUtils.equals("2802", statusCode)) {
                return setBaoqiList(stationPrefix, YLHXY04NUMBER, true, now, content);
            }
            if (StringUtils.equals("0001", statusCode) || StringUtils.equals("0002", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
            if (StringUtils.equals("0400", statusCode) || StringUtils.equals("0800", statusCode)) {
                return setBaoqiList(stationPrefix + "_2", now, content);
            }
            if (StringUtils.equals("1000", statusCode) || StringUtils.equals("2000", statusCode)) {
                return setBaoqiList(stationPrefix + "_3", now, content);
            }
        }
        //根据设备表记部分判断-河段5
        if (StringUtils.equals("594c4858593035", stationCode)) {
            String stationPrefix = "YLHXY05";

            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, YLHXY05NUMBER, false, now, content);
            }
            if (StringUtils.equals("1500", statusCode) || StringUtils.equals("2a00", statusCode)) {
                return setBaoqiList(stationPrefix, YLHXY05NUMBER, true, now, content);
            }
            if (StringUtils.equals("0100", statusCode) || StringUtils.equals("0200", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
            if (StringUtils.equals("0400", statusCode) || StringUtils.equals("0800", statusCode)) {
                return setBaoqiList(stationPrefix + "_2", now, content);
            }
            if (StringUtils.equals("1000", statusCode) || StringUtils.equals("2000", statusCode)) {
                return setBaoqiList(stationPrefix + "_3", now, content);
            }
        }
        //根据设备表记部分判断-河段6
        if (StringUtils.equals("594c4858593036", stationCode)) {
            String stationPrefix = "YLHXY06";

            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, YLHXY06NUMBER, false, now, content);
            }
            if (StringUtils.equals("1500", statusCode) || StringUtils.equals("2a00", statusCode)) {
                return setBaoqiList(stationPrefix, YLHXY06NUMBER, true, now, content);
            }
            if (StringUtils.equals("0100", statusCode) || StringUtils.equals("0200", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
            if (StringUtils.equals("0400", statusCode) || StringUtils.equals("0800", statusCode)) {
                return setBaoqiList(stationPrefix + "_2", now, content);
            }
            if (StringUtils.equals("1000", statusCode) || StringUtils.equals("2000", statusCode)) {
                return setBaoqiList(stationPrefix + "_3", now, content);
            }
        }


        return Collections.emptyList();
    }

    /**
     * 解析龙河曝气报文
     * @param content
     * @return List
     */
    private List<YlhBaoqi> setLhBaoqi(String content) {
        //截取设备标记部分
        String stationCode = StringUtils.substring(content, 0, 8);
        //截取状态值部分
        String statusCode = StringUtils.substring(content, 14, 18);

        Date now = new Date();
        //根据设备表记部分判断-河段1
        if (StringUtils.equals("4c483031", stationCode)) {

            String stationPrefix = "LH01";
            //判断状态值部分
            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, LH01NUMBER, false, now, content);
            }
            if (StringUtils.equals("1500", statusCode) || StringUtils.equals("2a00", statusCode)) {
                return setBaoqiList(stationPrefix, LH01NUMBER, true, now, content);
            }
            if (StringUtils.equals("0100", statusCode) || StringUtils.equals("0200", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
            if (StringUtils.equals("0400", statusCode) || StringUtils.equals("0800", statusCode)) {
                return setBaoqiList(stationPrefix + "_2", now, content);
            }

            if (StringUtils.equals("1000", statusCode) || StringUtils.equals("2000", statusCode)) {
                return setBaoqiList(stationPrefix + "_3", now, content);
            }
        }
        //根据设备表记部分判断-河段2
        if (StringUtils.equals("4c483032", stationCode)) {
            String stationPrefix = "LH02";
            //判断状态值部分
            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, LH02NUMBER, false, now, content);
            }
            if (StringUtils.equals("5500", statusCode) || StringUtils.equals("aa00", statusCode)) {
                return setBaoqiList(stationPrefix, LH02NUMBER, true, now, content);
            }
            if (StringUtils.equals("0100", statusCode) || StringUtils.equals("0200", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
            if (StringUtils.equals("0400", statusCode) || StringUtils.equals("0800", statusCode)) {
                return setBaoqiList(stationPrefix + "_2", now, content);
            }

            if (StringUtils.equals("1000", statusCode) || StringUtils.equals("2000", statusCode)) {
                return setBaoqiList(stationPrefix + "_3", now, content);
            }
            if (StringUtils.equals("4000", statusCode) || StringUtils.equals("8000", statusCode)) {
                return setBaoqiList(stationPrefix + "_4", now, content);
            }
        }
        //根据设备表记部分判断-河段3
        if (StringUtils.equals("4c483033", stationCode)) {
            String stationPrefix = "LH03";
            //判断状态值部分
            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, LH03NUMBER, false, now, content);
            }
            if (StringUtils.equals("1500", statusCode) || StringUtils.equals("2a00", statusCode)) {
                return setBaoqiList(stationPrefix, LH03NUMBER, true, now, content);
            }
            if (StringUtils.equals("0100", statusCode) || StringUtils.equals("0200", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
            if (StringUtils.equals("0400", statusCode) || StringUtils.equals("0800", statusCode)) {
                return setBaoqiList(stationPrefix + "_2", now, content);
            }

            if (StringUtils.equals("1000", statusCode) || StringUtils.equals("2000", statusCode)) {
                return setBaoqiList(stationPrefix + "_3", now, content);
            }
        }

        //根据设备表记部分判断-河段4
        if (StringUtils.equals("4c483034", stationCode)) {
            String stationPrefix = "LH04";
            //判断状态值部分
            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, LH04NUMBER, false, now, content);
            }
            if (StringUtils.equals("0500", statusCode) || StringUtils.equals("0a00", statusCode)) {
                return setBaoqiList(stationPrefix, LH04NUMBER, true, now, content);
            }
            if (StringUtils.equals("0400", statusCode) || StringUtils.equals("0800", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
            if (StringUtils.equals("0100", statusCode) || StringUtils.equals("0200", statusCode)) {
                return setBaoqiList(stationPrefix + "_2", now, content);
            }
        }

        //根据设备表记部分判断-河段5
        if (StringUtils.equals("4c483035", stationCode)) {
            String stationPrefix = "LH05";
            //判断状态值部分
            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, LH05NUMBER, false, now, content);
            }
            if (StringUtils.equals("0501", statusCode) || StringUtils.equals("aa02", statusCode)) {
                return setBaoqiList(stationPrefix, LH05NUMBER, true, now, content);
            }
            if (StringUtils.equals("0100", statusCode) || StringUtils.equals("0200", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
            if (StringUtils.equals("0400", statusCode) || StringUtils.equals("0800", statusCode)) {
                return setBaoqiList(stationPrefix + "_2", now, content);
            }
            if (StringUtils.equals("1000", statusCode) || StringUtils.equals("2000", statusCode)) {
                return setBaoqiList(stationPrefix + "_3", now, content);
            }
            if (StringUtils.equals("4000", statusCode) || StringUtils.equals("8000", statusCode)) {
                return setBaoqiList(stationPrefix + "_4", now, content);
            }
            if (StringUtils.equals("0001", statusCode) || StringUtils.equals("0002", statusCode)) {
                return setBaoqiList(stationPrefix + "_5", now, content);
            }
        }

        //根据设备表记部分判断-河段6
        if (StringUtils.equals("4c483036", stationCode)) {
            String stationPrefix = "LH06";
            //判断状态值部分
            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, LH06NUMBER, false, now, content);
            }
            if (StringUtils.equals("0100", statusCode) || StringUtils.equals("0200", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
        }
        //根据设备表记部分判断-河段6
        if (StringUtils.equals("4c483037", stationCode)) {
            String stationPrefix = "LH07";
            //判断状态值部分
            if (StringUtils.equals("0000", statusCode)) {
                return setBaoqiList(stationPrefix, LH07NUMBER, false, now, content);
            }
            if (StringUtils.equals("0500", statusCode) || StringUtils.equals("0a00", stationCode)) {
                return setBaoqiList(stationPrefix, LH07NUMBER, true, now, content);
            }
            if (StringUtils.equals("0400", statusCode) || StringUtils.equals("0800", statusCode)) {
                return setBaoqiList(stationPrefix + "_1", now, content);
            }
            if (StringUtils.equals("0100", statusCode) || StringUtils.equals("0200", statusCode)) {
                return setBaoqiList(stationPrefix + "_2", now, content);
            }
        }
        return Collections.emptyList();

    }

    /**
     * 多个曝气同时开关,赋值报文实体
     * @param stationPrefix
     * @param number
     * @param status
     * @param now
     * @param content
     * @return List
     */
    private List<YlhBaoqi> setBaoqiList(String stationPrefix, int number, boolean status, Date now, String content) {

        List<YlhBaoqi> ylhBaoqis = new ArrayList<>();
        for (int i = 1;i <= number;i++) {
            YlhBaoqi ylhBaoqi = new YlhBaoqi();
            ylhBaoqi.setStation(stationPrefix + "_" + i);
            ylhBaoqi.setStatus(status);
            ylhBaoqi.setTime(now);
            ylhBaoqi.setContent(content);
            ylhBaoqis.add(ylhBaoqi);
        }
        return ylhBaoqis;
    }

    /**
     * 单个曝气开关,赋值报文实体
     * @param station
     * @param now
     * @param content
     * @return List
     */
    private List<YlhBaoqi> setBaoqiList(String station, Date now, String content) {
        YlhBaoqi ylhBaoqi = new YlhBaoqi();
        ylhBaoqi.setStation(station);
        ylhBaoqi.setStatus(true);
        ylhBaoqi.setTime(now);
        ylhBaoqi.setContent(content);
        List<YlhBaoqi> ylhBaoqis = new ArrayList<>();
        ylhBaoqis.add(ylhBaoqi);
        return ylhBaoqis;
    }

}
